package com.doublefish.doublefishrobot.ui.home;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.ui.home.inerface.OnModeMenuRightBtnClickListner;
import com.doublefish.doublefishrobot.ui.home.s5.S5ModeMenuFragment;
import com.doublefish.doublefishrobot.ui.home.s6.S6ModeMenuFragment;
import com.doublefish.doublefishrobot.ui.home.s8.S8ModeMenuFragment;
import com.doublefish.doublefishrobot.utils.CrashReportUtils;
import com.doublefish.doublefishrobot.utils.ble.bean.ModeConstant;
import com.doublefish.doublefishrobot.widget.CommonDialog;


/**
 * 作者:dugaofeng on 2024/12/19 17:04
 */
public class HomeContentFragment extends Fragment implements OnModeMenuRightBtnClickListner {

    private View view;

    private HomeFragment homeFragment = new HomeFragment();
    private Fragment menuFragment = null;

    private int fragmentPosition = 0;  //当前显示的Fragment

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_home_content, container, false);
            initViews();
        }
        return view;
    }

    private void initViews() {
        homeFragment.setOnToModeMenuListner(new HomeFragment.OnToModeMenuListner() {
            @Override
            public void toModeMenu(int deviceType) {
                toModeMenuVC(deviceType);
            }
        });
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.fl_content, homeFragment);
        transaction.show(homeFragment).commit();
    }

    public void setOnCheckPermissonsListner(HomeFragment.OnCheckPermissonsListner onCheckPermissonsListner) {
        homeFragment.setOnCheckPermissonsListner(onCheckPermissonsListner);
    }

    private void toModeMenuVC(int deviceType) {
        if (isStateSaved() || getActivity() == null || getActivity().isFinishing()) {
            CrashReportUtils.postTestException("isStateSaved 0");
            return;
        }

        FragmentManager fm = getChildFragmentManager();
        if (fm.isStateSaved()) {
            CrashReportUtils.postTestException("isStateSaved 1");
            return;
        }
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.setCustomAnimations(R.anim.flip_from_right,R.anim.flip_exit);

        if (menuFragment != null) {
            transaction.remove(menuFragment);
        }
        switch (deviceType) {
            case ModeConstant.DeviceTypeS8: {
                fragmentPosition = 1;
                menuFragment = new S8ModeMenuFragment();
                ((S8ModeMenuFragment)menuFragment).setOnModeMenuRightBtnClickListner(this);
                transaction.add(R.id.fl_content, menuFragment)
                        .hide(homeFragment)
                        .show(menuFragment)
                        .commit();
            }
            break;

            case ModeConstant.DeviceTypeS6: {
                fragmentPosition = 2;
                menuFragment = new S6ModeMenuFragment();
                ((S6ModeMenuFragment)menuFragment).setOnModeMenuRightBtnClickListner(this);
                transaction.add(R.id.fl_content, menuFragment)
                        .hide(homeFragment)
                        .show(menuFragment)
                        .commit();
            }
            break;

            case ModeConstant.DeviceTypeS5: {
                fragmentPosition = 3;
                menuFragment = new S5ModeMenuFragment();
                ((S5ModeMenuFragment)menuFragment).setOnModeMenuRightBtnClickListner(this);
                transaction.add(R.id.fl_content, menuFragment)
                        .hide(homeFragment)
                        .show(menuFragment)
                        .commit();
            }
            break;
        }
    }

    @Override
    public void onModeMenuRightBtnClick() {
        fragmentPosition = 0;
        if (menuFragment == null) {
            return;
        }
        try {
            FragmentManager fm = getChildFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();
            transaction.setCustomAnimations(R.anim.flip_exit, R.anim.flip_from_left);
            transaction.remove(menuFragment).show(homeFragment).commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //返回手势处理结果
    //true  表示自己弹窗提示    false  表示有app处理
    public boolean onBackKeyDown() {
        if (fragmentPosition == 0) {
            return false;
        }
        showBackAlert();
        return true;
    }

    private void showBackAlert() {
        //询问连接新设备
        String title = getString(R.string.disconnect_alert);
        CommonDialog dialog = new CommonDialog(getActivity(), title, null, new CommonDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    notiChildrenBackAction();
                }
            }
        });
        dialog.setPositiveButton(getString(R.string.disconnect));
        dialog.show();
    }

    private void notiChildrenBackAction() {
        if (fragmentPosition == 0) {
            return;
        }
        if (fragmentPosition == 1) {
            ((S8ModeMenuFragment)menuFragment).disConnect();
            return;
        }
        if (fragmentPosition == 2) {
            ((S6ModeMenuFragment)menuFragment).disConnect();
            return;
        }
        if (fragmentPosition == 3) {
            ((S5ModeMenuFragment)menuFragment).disConnect();
        }
    }
}
